package com.cn.crmsystem.controller.systemController;

import com.cn.crmsystem.pojo.Login;
import com.cn.crmsystem.pojo.ResultJson;
import com.cn.crmsystem.pojo.User;
import com.cn.crmsystem.service.systemService.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(value = "用户数据test", tags = "用户增删改查数据接口API")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/selectAllUser",method = RequestMethod.POST)
    @ApiOperation(value = "员工查询接口", notes = "管理员账号查询所有员工信息",httpMethod = "POST",response = ResultJson.class)
    public ResultJson selectUser(@RequestBody
        @ApiParam(name = "map",value = "{name 用户名(String), departmentName 部门名称(String), user_state 用户状态(int 0:启用;2:禁用)}") Map map){
        return userService.selectUser(map);
    }
    /**
     * 注册
     */

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ApiOperation(value = "员工注册接口", notes = "员工注册账号", httpMethod = "POST", response = ResultJson.class)
    public ResultJson regUser(@RequestParam
                                          @ApiParam(name = "username",value = "用户名") String username,
                              @RequestParam @ApiParam(name = "password",value = "密码")String password){
        System.out.println("-----注册-----");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        return userService.addUser(username, md5(username,password));
    }

    // 注册时，进行shiro加密，返回加密后的结果，如果在加入shiro之前，存在用户密码不是此方式加密的，那么将无法登录
    // 使用用户名作为盐值
    private String md5(String username, String password) {
        //加密方式
        String hasAlgorithmName = "MD5";
        //账号作为盐值
        ByteSource salt = ByteSource.Util.bytes(username);
        //加密次数
        int hashIterations = 11;
        return new SimpleHash(hasAlgorithmName, password, salt, hashIterations).toString();
    }

    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    @ApiOperation(value = "员工删除接口", notes = "管理员删除员工账号",httpMethod = "POST",response = ResultJson.class)
    public ResultJson deleteUser(@RequestParam
                                             @ApiParam(name = "id", value = "员工编号") Integer id){
        System.out.println("删除id为" + id + "的用户");
        return userService.deleteUser(id);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteManyUser",method = RequestMethod.POST)
    @ApiOperation(value ="批量删除用户数据接口",notes = "传入要删除的数据id",httpMethod = "post",response = ResultJson.class)
    @ApiImplicitParam(name = "ids",value = "用户id",paramType = "query",dataType = "int",required = false)
    public ResultJson deleteManyUser(Integer[] ids){
        System.out.println(ids.length);
        return userService.deleteManyUser(ids);
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ApiOperation(value = "员工信息修改接口", notes = "管理员或员工修改信息",httpMethod = "POST", response = ResultJson.class)
    public ResultJson updateUser(@RequestBody
        @ApiParam(name = "map",value = "{name 用户名(String)," +
                "phone 电话号码(String)," +
                "sex 性别(int 0:男;1:女)," +
                "user_head_img 头像 (String)," +
                "role_id 角色id(int)," +
                "user_state 账号状态(0:启用;1:禁用)," +
                "dept_id 部门id (int)," +
                "operator_id 操作人id(通过token传)," +
                "id 当前数据的编号(int)" +
                "}",required = false) Map map){

        Login login = (Login)SecurityUtils.getSubject().getPrincipal();
        System.out.println("-=-=-=-=-==-=-=="+login);
//        Login login = new Login();
//        login.setUser_id(1);
        map.put("operator_id",login.getUser_id());
        return userService.updateUser(map);
    }

}
